import http from '@/utils/http';
import type { ApiPaginatedResponse, ApiResponse } from '@/types';

// 订单相关类型定义
export interface Order {
  id: number;
  order_number: string;
  user_id: number;
  total_amount: number;
  status: string;
  payment_status: string;
  payment_method?: string;
  shipping_address?: string;
  notes?: string;
  created_at: string;
  updated_at: string;
  username?: string;
  email?: string;
  nickname?: string;
  phone?: string;
  customerInfo?: {
    username: string;
    email: string;
    nickname?: string;
    phone?: string;
  };
  itemStats?: {
    itemCount: number;
    totalQuantity: number;
  };
  items?: OrderItem[];
  summary?: {
    itemCount: number;
    totalQuantity: number;
    subtotal: number;
    totalAmount: number;
  };
}

export interface OrderItem {
  id: number;
  order_id: number;
  product_id?: number;
  config_id?: number;
  product_name?: string;
  config_name?: string;
  quantity: number;
  unit_price: number;
  productInfo?: {
    id: number;
    name: string;
    image_url?: string;
    category?: string;
  };
  configInfo?: {
    id: number;
    name: string;
    image_url?: string;
    category?: string;
  };
}

export interface OrderQueryParams {
  page?: number;
  limit?: number;
  keyword?: string;
  status?: string;
  paymentStatus?: string;
  startDate?: string;
  endDate?: string;
  minAmount?: number;
  maxAmount?: number;
}

export interface UpdateOrderStatusRequest {
  status?: string;
  payment_status?: string;
  notes?: string;
}

export interface BatchUpdateStatusRequest {
  ids: number[];
  status?: string;
  payment_status?: string;
}

export interface OrderStatistics {
  orders: {
    total_orders: number;
    pending_orders: number;
    confirmed_orders: number;
    processing_orders: number;
    shipped_orders: number;
    delivered_orders: number;
    completed_orders: number;
    cancelled_orders: number;
    total_revenue: number;
    avg_order_value: number;
    orders_this_week: number;
    orders_this_month: number;
  };
  payments: {
    pending_payments: number;
    paid_orders: number;
    failed_payments: number;
    refunded_orders: number;
    paid_revenue: number;
  };
  dailyStats: Array<{
    date: string;
    order_count: number;
    daily_revenue: number;
  }>;
  topProducts: Array<{
    product_name: string;
    total_sold: number;
    total_revenue: number;
    order_count: number;
  }>;
}

/**
 * 获取订单列表
 */
export const getOrderList = async (params?: OrderQueryParams): Promise<ApiPaginatedResponse<Order>> => {
  const response = await http.get('/admin/orders', { params });
  return response.data;
};

/**
 * 获取订单详情
 */
export const getOrderDetail = async (id: number): Promise<ApiResponse<Order>> => {
  const response = await http.get(`/admin/orders/${id}`);
  return response.data;
};

/**
 * 更新订单状态
 */
export const updateOrderStatus = async (id: number, data: UpdateOrderStatusRequest): Promise<ApiResponse<Order>> => {
  const response = await http.put(`/admin/orders/${id}/status`, data);
  return response.data;
};

/**
 * 批量更新订单状态
 */
export const batchUpdateOrderStatus = async (data: BatchUpdateStatusRequest): Promise<ApiResponse<any>> => {
  const response = await http.post('/admin/orders/batch-update-status', data);
  return response.data;
};

/**
 * 删除订单
 */
export const deleteOrder = async (id: number): Promise<ApiResponse<any>> => {
  const response = await http.delete(`/admin/orders/${id}`);
  return response.data;
};

/**
 * 获取订单统计信息
 */
export const getOrderStatistics = async (): Promise<ApiResponse<OrderStatistics>> => {
  const response = await http.get('/admin/orders/statistics/overview');
  return response.data;
}; 